-- MRLC_GameplyScripts
-- Author: yiboy
-- DateCreated: 12/1/2024 1:16:49 PM
--------------------------------------------------------------
-- ===========================================================================
--	INCLUDE
-- ===========================================================================
include("MRLC_SupportFunctions")

-- ===========================================================================
--	CONST
-- ===========================================================================
local GAME_SPEED = GameConfiguration.GetGameSpeedType();
local GAME_SPEED_MULTIPLIER = GameInfo.GameSpeeds[GAME_SPEED] and GameInfo.GameSpeeds[GAME_SPEED].CostMultiplier / 100 or 1;

local FREE_TECH_KEY = 'HD_FREE_TECH';
local FREE_CIVIC_KEY = 'HD_FREE_CIVIC';
--========================================================================================================================
-- FUNC
--========================================================================================================================
	function MayaReworkLongCount_SetProperty(iPlayer, kParam)
		local pPlayer = Players[iPlayer]
		local Key = kParam.Key
		local Value = kParam.Value

		pPlayer:SetProperty(Key,Value)

		if string.find(Key, "MRLC_YIELD_") then
			MayaReworkLongCount_SetPlotProperty(iPlayer, true, Key)
		end
	end

	function MayaReworkLongCount_SetGameProperty(iPlayer, kParam)
		local Key = kParam.Key
		local Value = kParam.Value

		Game:SetProperty(Key,Value)
	end

	function MayaReworkLongCount_ChangeGPP(iPlayer, kParam)
		local pPlayer = Players[iPlayer]

		local hClassID	= kParam.hClassID
		local hCost		= kParam.hCost

		local nGPPGained = hCost

		--增加100%花费的点数即可
		pPlayer:GetGreatPeoplePoints():ChangePointsTotal(hClassID, nGPPGained);

		--然后增加一个已使用点数
		local m_propertyKey_3				= "MayaRework_LongCount_PointsUsed";
		local property = pPlayer:GetProperty(m_propertyKey_3) or 0;
		pPlayer:SetProperty(m_propertyKey_3, property+1)

		print("free recruit 002")

	end

--========================================================================================================================
	function On_PlayerTurnActivated_MRLC(playerID, bIsFirstTime)
		if not bIsFirstTime then
			return
		end
		MayaReworkLongCount_SetPlotProperty(playerID, false, "")
	end

	function On_CityProductionCompleted_MRLC(playerID, cityID, productionID, objectID, bCancelled)
		MayaReworkLongCount_SetPlotProperty(playerID, false, "")
	end

	function MayaReworkLongCount_SetPlotProperty(playerID, forceSet, forceSetKey)
		
		local pPlayer = Players[ playerID ];
		if pPlayer == nil then
			return
		end

		--获取允许的产出类型
		local yield_types = {}

		for row in GameInfo.MayaUA_GreatPersonClasses() do
			if not TableIncludeValue(yield_types, row.YieldType) then
				table.insert(yield_types, row.YieldType)
			end
		end

		--遍历玩家的区域单元格
		local pPlayerDistricts = pPlayer:GetDistricts();
		for _, district in pPlayerDistricts:Members() do
			--local pCity = district:GetCity();
			local districtID = district:GetID();
			local pPlot = Map.GetPlot(district:GetX(), district:GetY());

			for k, yield_type in pairs(yield_types) do
				local property_key = "MRLC_" .. yield_type .."_PROPERTY"

				local player_property = pPlayer:GetProperty(property_key) or 0
				if player_property > 0 or (forceSet and forceSetKey==property_key) then
					pPlot:SetProperty(property_key, 1)
				end
			end
			
		end
		
	end

--========================================================================================================================

	onSixSkyGetLongCountGPP = {
		-- 长纪历提供首个大科学家及大艺术家时，分别奖励1项当前可以研究的科技（点击科技树上的科技图标来选择被奖励的科技）及市政；
		-- 提供大工程师时，为所有在建奇观立即提供20% [ICON_PRODUCTION] 生产力；
		-- 提供大农业家时，为所有城市立即提供一个建造者。
		GREAT_PERSON_CLASS_ENGINEER = function(playerID)
			local pPlayer = Players[playerID]
			for _, pCity in pPlayer:GetCities():Members() do
    			local buildQueue = pCity:GetBuildQueue()
				if buildQueue then
    				local currentProduction = buildQueue:CurrentlyBuilding()
					if currentProduction then
    					local buildingInfo = GameInfo.Buildings[currentProduction]
						if buildingInfo and buildingInfo.IsWonder then
							local WonderCost = math.floor(buildingInfo.Cost * GAME_SPEED_MULTIPLIER);
    						pCity:GetBuildQueue():AddProgress(WonderCost * 0.2);
						end
					end
				end
			end
		end,
		GREAT_PERSON_CLASS_SCIENTIST = function(playerID, m_IsHDActive)
			if m_IsHDActive then
				local pPlayer = Players[playerID]
				local remains = pPlayer:GetProperty(FREE_TECH_KEY) or 0;
				pPlayer:SetProperty(FREE_TECH_KEY, remains + 1);
			end
		end,
		GREAT_PERSON_CLASS_ARTIST = function(playerID, m_IsHDActive)
			if m_IsHDActive then
				local pPlayer = Players[playerID]
				local remains = pPlayer:GetProperty(FREE_CIVIC_KEY) or 0;
				pPlayer:SetProperty(FREE_CIVIC_KEY, remains + 1);
			end
		end,
		GREAT_PERSON_CLASS_AGRONOMIST = function(playerID)
			local pPlayer = Players[playerID]
			for _, pCity in pPlayer:GetCities():Members() do
    			UnitManager.InitUnit(playerID, "UNIT_BUILDER", pCity:GetX(), pCity:GetY())
			end
		end
	}
--========================================================================================================================
	function SixLeader_MayaReworkLongCount(playerID, kParam)

		local pPlayer = Players[ playerID ];
		local classTypeStr = kParam.classTypeStr
		if pPlayer == nil then
			return
		end
		if onSixSkyGetLongCountGPP[classTypeStr] then
			onSixSkyGetLongCountGPP[classTypeStr](playerID, kParam.m_IsHDActive)
			pPlayer:SetProperty('onSixSkyGetLongCountGPP'..classTypeStr, 1)
		end

	end

--========================================================================================================================
-- Register
--========================================================================================================================
function Initialize()

	-- 文明能力
	GameEvents.MayaReworkLongCount_SetProperty.Add(		MayaReworkLongCount_SetProperty		)
	GameEvents.MayaReworkLongCount_SetGameProperty.Add( MayaReworkLongCount_SetGameProperty	)
	GameEvents.MayaReworkLongCount_ChangeGPP.Add(		MayaReworkLongCount_ChangeGPP		)

	-- 领袖能力
	GameEvents.MayaReworkLongCount_SetProperty.Add(		SixLeader_MayaReworkLongCount		)


	Events.PlayerTurnActivated.Add( On_PlayerTurnActivated_MRLC )
	Events.CityProductionCompleted.Add( On_CityProductionCompleted_MRLC )
end

Events.LoadGameViewStateDone.Add(Initialize);
print("Maya Rework Long Count Gameplay Initialized!")